# Add a python test from a python file
# One cannot simply do:
# SET(ENV{PYTHONPATH} ${LIBRARY_OUTPUT_PATH})
# SET(my_test "from test_mymodule import *\;test_mymodule()")
# ADD_TEST(PYTHON-TEST-MYMODULE  python -c ${my_test})
# Since cmake is only transmitting the ADD_TEST line to ctest thus you are loosing
# the env var. The only way to store the env var is to physically write in the cmake script
# whatever PYTHONPATH you want and then add the test as 'cmake -P python_test.cmake'
# 
# Usage:
# SET_SOURCE_FILES_PROPERTIES(test.py PROPERTIES PYTHONPATH
#   "${LIBRARY_OUTPUT_PATH}:${VTK_DIR}")
# ADD_PYTHON_TEST(PYTHON-TEST test.py)
#
#  Copyright (c) 2006-2009 Mathieu Malaterre <mathieu.malaterre@gmail.com>
#
#  Redistribution and use is allowed according to the terms of the New
#  BSD license.
#  For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#

# Modified by Zacarias Ojeda <zojeda@gmail.com>

MACRO(ADD_JAVA_TEST TESTNAME FILENAME)
  GET_SOURCE_FILE_PROPERTY(loc ${FILENAME} LOCATION)
  GET_SOURCE_FILE_PROPERTY(jvmenv ${FILENAME} RUNTIMEPATH)
  GET_FILENAME_COMPONENT(loc2 ${loc} NAME_WE)


  IF(CMAKE_CONFIGURATION_TYPES)
    # I cannot use CMAKE_CFG_INTDIR since it expand to "$(OutDir)"
    IF(jvmenv)
      SET(jvmenv "${jvmenv};${LIBRARY_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}")
    ELSE(jvmenv)
      SET(jvmenv ${LIBRARY_OUTPUT_PATH}/${CMAKE_BUILD_TYPE})
	    #SET(jvmenv ${LIBRARY_OUTPUT_PATH}/${CMAKE_CFG_INTDIR})
	    #SET(jvmenv ${LIBRARY_OUTPUT_PATH}/${CMAKE_CONFIG_TYPE})
	    #SET(jvmenv ${LIBRARY_OUTPUT_PATH}/\${CMAKE_CONFIG_TYPE})
    ENDIF(jvmenv)
  ELSE(CMAKE_CONFIGURATION_TYPES)
    IF(jvmenv)
      SET(jvmenv ${jvmenv}:${LIBRARY_OUTPUT_PATH})
    ELSE(jvmenv)
      SET(jvmenv ${LIBRARY_OUTPUT_PATH})
    ENDIF(jvmenv)
   ENDIF(CMAKE_CONFIGURATION_TYPES)
  STRING(REGEX REPLACE ";" " " wo_semicolumn "${ARGN}")
  FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake
"
  SET(ENV{LD_LIBRARY_PATH} ${jvmenv})
#  SET(ENV{CLASSPATH} ${jvmenv}/gdcm.jar:.)
  MESSAGE(\"jvmenv: ${jvmenv}\")
  MESSAGE(\"loc: ${loc}\")
  MESSAGE(\"loc2: ${loc2}\")
  MESSAGE( \"wo_semicolumn: ${wo_semicolumn}\" )
  MESSAGE("Compiling Source File" )

  EXECUTE_PROCESS(
  	COMMAND ${JAVA_COMPILE} ${loc} -d ${CMAKE_CURRENT_BINARY_DIR} -cp ${jvmenv} ${wo_semicolumn}
  	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  	RESULT_VARIABLE result
  	OUTPUT_VARIABLE output
  	ERROR_VARIABLE  error
  )
  
  # Pass the output back to ctest
  IF(output)
    MESSAGE("\${output}")
  ENDIF(output)
  IF(error)
    MESSAGE(SEND_ERROR "\${error}")
  ENDIF(error)

  MESSAGE("Running Compiled File" )

  EXECUTE_PROCESS(
  	COMMAND ${JAVA_RUNTIME} ${loc2}
  	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  	RESULT_VARIABLE result
  	OUTPUT_VARIABLE output
  	ERROR_VARIABLE  error
  )
  
  # Pass the output back to ctest
  IF(output)
    MESSAGE("\${output}")
  ENDIF(output)
  IF(error)
    MESSAGE(SEND_ERROR "\${error}")
  ENDIF(error)
"
)
  ADD_TEST(${TESTNAME} ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake)
ENDMACRO(ADD_JAVA_TEST)

# Byte compile recursively a directory (DIRNAME)
#MACRO(ADD_PYTHON_COMPILEALL_TEST DIRNAME)
#  # First get the path:
#  GET_FILENAME_COMPONENT(temp_path "${PYTHON_LIBRARIES}" PATH)
#  # Find the python script:
#  GET_FILENAME_COMPONENT(PYTHON_COMPILE_ALL_PY "${temp_path}/../compileall.py" ABSOLUTE)
#  # add test, use DIRNAME to create uniq name for the test:
#  ADD_TEST(COMPILE_ALL-${DIRNAME} ${PYTHON_EXECUTABLE} "${PYTHON_COMPILE_ALL_PY}" -q ${DIRNAME})
#ENDMACRO(ADD_PYTHON_COMPILEALL_TEST)
#

